//package com.pearadmin.modules.pro.controller;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.node.ObjectNode;
//import com.pearadmin.common.web.base.BaseController;
//import io.swagger.annotations.Api;
//import org.activiti.editor.constants.ModelDataJsonConstants;
//import org.activiti.engine.ActivitiException;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.repository.Model;
//import org.apache.batik.transcoder.TranscoderInput;
//import org.apache.batik.transcoder.TranscoderOutput;
//import org.apache.batik.transcoder.image.PNGTranscoder;
//import org.apache.commons.io.IOUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.http.HttpStatus;
//import org.springframework.web.bind.annotation.*;
//
//import javax.annotation.Resource;
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.InputStream;
//import java.nio.charset.StandardCharsets;
//
///**
// * Describe: 流程编辑器控制器
// * Author: 就眠仪式
// * createTime: 2019/10/23
// */
//@RestController
//@Api(tags = {"流程设计"})
//@RequestMapping("service")
//public class ProEditorController extends BaseController implements ModelDataJsonConstants {
//
//    @Resource
//    private RepositoryService repositoryService;
//
//    @Resource
//    private ObjectMapper objectMapper;
//
//    /**
//     * 根据 modelId 获取流程模型
//     *
//     * @param modelId 模型ID
//     * @return {@link ObjectNode}
//     */
//    @RequestMapping(value = "/model/{modelId}/json", method = RequestMethod.GET, produces = "application/json")
//    public ObjectNode getEditorJson(@PathVariable String modelId) {
//        ObjectNode modelNode = null;
//        Model model = repositoryService.getModel(modelId);
//        if (model != null) {
//            try {
//                if (StringUtils.isNotEmpty(model.getMetaInfo())) {
//                    modelNode = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
//                } else {
//                    modelNode = objectMapper.createObjectNode();
//                    modelNode.put(MODEL_NAME, model.getName());
//                }
//                modelNode.put(MODEL_ID, model.getId());
//                ObjectNode editorJsonNode = (ObjectNode) objectMapper.readTree(
//                        new String(repositoryService.getModelEditorSource(model.getId()), StandardCharsets.UTF_8));
//                modelNode.replace("model", editorJsonNode);
//            } catch (Exception e) {
//                throw new ActivitiException("Error creating model JSON", e);
//            }
//        }
//        return modelNode;
//    }
//
//    /**
//     * 保存流程模型
//     *
//     * @param modelId     模型ID
//     * @param name        流程模型名称
//     * @param description 流程描述
//     * @param json_xml     流程文件
//     * @param svg_xml      流程图片
//     */
//    @RequestMapping(value = "/model/{modelId}/save", method = RequestMethod.PUT)
//    @ResponseStatus(value = HttpStatus.OK)
//    public void saveModel(@PathVariable String modelId, String name, String description,String json_xml, String svg_xml) {
//        try {
//            Model model = repositoryService.getModel(modelId);
//            ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
//            modelJson.put(MODEL_NAME, name);
//            modelJson.put(MODEL_DESCRIPTION, description);
//            model.setMetaInfo(modelJson.toString());
//            model.setName(name);
//            repositoryService.saveModel(model);
//            repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes(StandardCharsets.UTF_8));
//
//            InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes(StandardCharsets.UTF_8));
//            TranscoderInput input = new TranscoderInput(svgStream);
//            PNGTranscoder transcoder = new PNGTranscoder();
//            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//            TranscoderOutput output = new TranscoderOutput(outStream);
//
//            transcoder.transcode(input, output);
//            final byte[] result = outStream.toByteArray();
//            repositoryService.addModelEditorSourceExtra(model.getId(), result);
//            outStream.close();
//        } catch (Exception e) {
//            throw new ActivitiException("Error saving model", e);
//        }
//    }
//
//    /**
//     * 流程工具列表
//     *
//     * @return {@link String}
//     */
//    @ResponseBody
//    @GetMapping(value = "/editor/stencilset", produces = "application/json;charset=utf-8")
//    public String getStencilset() {
//        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json");
//        try {
//            return IOUtils.toString(stream, StandardCharsets.UTF_8);
//        } catch (Exception e) {
//            throw new ActivitiException("Error while loading stencil set", e);
//        }
//    }
//
//}
